package chapter2;

import java.lang.management.RuntimeMXBean;

import sun.management.ManagementFactory;
import chapter3.Battery;
import chapter3.EnergySource;

public class Vehicle implements Cloneable {
    static long nextID;
    static final String TURN_LEFT = "left";
    static final String TURN_RIGHT = "right"; 
    
    private long idNum;
    private double speed;
    private double direction;
    private String owner;
    
    private EnergySource energy;
    
    public Vehicle(){
        idNum = nextID++;
        this.energy = new Battery();
    }
    
    public Vehicle(String owner){
        this.owner = owner;
    }
    
    public Vehicle(EnergySource energy,String owner){
        this();
        this.owner = owner;
    }
    
    public String toString(){
        String desc = idNum + " (" + owner + ") " + speed +"km";
        return desc;
    }
    
    public void changeSpeed(double speed){
        this.speed = speed;
    }
    
    public void start(){
        if(energy.isEmpty()){
            System.out.println("Can't start, energy source is empty");
        }else{
            System.out.println("Engine start...");
        }
    }
    
    public void stop(){
        this.speed = 0.0;
    }
    
    public void turn(double direction){
        this.direction +=  direction;
    }
    
    public Vehicle clone(){
        Vehicle nObj;
        try {
            nObj = (Vehicle)super.clone();
            nObj.energy = energy.clone();
            return nObj;
        } catch (CloneNotSupportedException e) {
            //can't happen
            return null;
        }
        
    }
    
    public native int getCPUID();
    
    static long highestIDNum(){
        return nextID;
    }
    
    public static void main(String[] args){
        Vehicle bus = new Vehicle();
        bus.idNum = Vehicle.nextID++;
        bus.owner = "wei";
        //bus.getCPUID();
        
        Vehicle bicycle = new Vehicle();
        bicycle.idNum = Vehicle.nextID++;
        bicycle.owner = "apple";
        
        
        System.out.println(bus.idNum + "=" + bus.owner + " " + bicycle.idNum + "=" + bicycle.owner);
        
        System.out.println(System.getProperty("java.vendor"));
        RuntimeMXBean rmx = ManagementFactory.getRuntimeMXBean();
        System.out.println(rmx.getName());
    }
}
